package com.guapi.service;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.guapi.model.Content;
import com.guapi.model.ContentSearchResult;
import com.guapi.model.Tag;

public class JaxbQueryResultUnmarshaller implements QueryResultUnmarshaller {

	private Unmarshaller unmarshaller;

	public JaxbQueryResultUnmarshaller() {
		try {
			this.unmarshaller = JAXBContext
									.newInstance(ContentSearchResult.class,
											Content.class,
											Tag.class)
									.createUnmarshaller();
		} catch (JAXBException e) {
			throw new RuntimeException("Error binding xml", e);
		}
	}

	public ContentSearchResult unmarshall(InputStream inputStream) throws ApiUnmarshallException {
		try {
			return (ContentSearchResult) unmarshaller.unmarshal(inputStream);
		} catch (JAXBException e) {
			throw new ApiUnmarshallException("Unable to unmarshal", e);
		}
	}

}
